// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.

package org.jetbrains.kotlin.idea.base.projectStructure.forwardDeclarations

import com.intellij.platform.workspace.jps.entities.LibraryEntity
import com.intellij.platform.workspace.storage.EntitySource
import com.intellij.platform.workspace.storage.EntityType
import com.intellij.platform.workspace.storage.GeneratedCodeApiVersion
import com.intellij.platform.workspace.storage.MutableEntityStorage
import com.intellij.platform.workspace.storage.WorkspaceEntity
import com.intellij.platform.workspace.storage.annotations.Child
import com.intellij.platform.workspace.storage.impl.containers.toMutableWorkspaceSet
import com.intellij.platform.workspace.storage.url.VirtualFileUrl

/**
 * A child entity for [LibraryEntity] that stores K/N forward declaration roots of libraries that declare them.
 * Implementation of the interface and commented regions are generated by the Workspace Model Generator.
 * Use the "Generate Workspace Model Implementation" action for the module after changes in the interface.
 * More information in the [Workspace Model documentation](https://youtrack.jetbrains.com/articles/IJPL-A-52)
 *
 * @see [org.jetbrains.kotlin.idea.base.projectStructure.KtNativeKlibLibraryModuleByModuleInfo].
 */
interface KotlinForwardDeclarationsWorkspaceEntity : WorkspaceEntity {
    val forwardDeclarationRoots: Set<VirtualFileUrl>
    val library: LibraryEntity

    //region generated code
    @GeneratedCodeApiVersion(3)
    interface Builder : WorkspaceEntity.Builder<KotlinForwardDeclarationsWorkspaceEntity> {
        override var entitySource: EntitySource
        var forwardDeclarationRoots: MutableSet<VirtualFileUrl>
        var library: LibraryEntity.Builder
    }

    companion object : EntityType<KotlinForwardDeclarationsWorkspaceEntity, Builder>() {
        @JvmOverloads
        @JvmStatic
        @JvmName("create")
        operator fun invoke(
            forwardDeclarationRoots: Set<VirtualFileUrl>,
            entitySource: EntitySource,
            init: (Builder.() -> Unit)? = null,
        ): Builder {
            val builder = builder()
            builder.forwardDeclarationRoots = forwardDeclarationRoots.toMutableWorkspaceSet()
            builder.entitySource = entitySource
            init?.invoke(builder)
            return builder
        }
    }
    //endregion
}

//region generated code
fun MutableEntityStorage.modifyKotlinForwardDeclarationsWorkspaceEntity(
    entity: KotlinForwardDeclarationsWorkspaceEntity,
    modification: KotlinForwardDeclarationsWorkspaceEntity.Builder.() -> Unit,
): KotlinForwardDeclarationsWorkspaceEntity {
    return modifyEntity(KotlinForwardDeclarationsWorkspaceEntity.Builder::class.java, entity, modification)
}

var LibraryEntity.Builder.kotlinForwardDeclarationsWorkspaceEntity: @Child KotlinForwardDeclarationsWorkspaceEntity.Builder?
        by WorkspaceEntity.extensionBuilder(KotlinForwardDeclarationsWorkspaceEntity::class.java)
//endregion

val LibraryEntity.kotlinForwardDeclarationsWorkspaceEntity: @Child KotlinForwardDeclarationsWorkspaceEntity?
    by WorkspaceEntity.extension()

object KotlinFwdWorkspaceEntitySource: EntitySource